package com.mayi.zds.web;

import com.alibaba.fastjson.JSON;
import com.mayi.zds.entity.Message;
import com.mayi.zds.entity.Order;
import com.mayi.zds.entity.PayBean;
import com.mayi.zds.service.MessageService;
import com.mayi.zds.service.OrderService;
import com.mayi.zds.service.PayService;
import com.mayi.zds.service.imp.ScanOrderStatusServiceImp;
import com.mayi.zds.service.imp.ZdsWebsocketHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.socket.TextMessage;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @ClassName OrderController
 * @Author zhong
 * @Date 2019/12/26 10:02:45
 * @Version 1.0
 */
@Controller
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private OrderService orderService;

    @Autowired
    private PayService payService;

    @Autowired
    private ScanOrderStatusServiceImp scanOrderStatusService;

    @Autowired
    private ZdsWebsocketHandler zdsWebsocketHandler;

    @Autowired
    private MessageService messageService;

    /**
     * 添加订单，需要包含优惠券列表CouponList,订单项列表OrderItemList
     *
     * @Author zhong
     * @Date 2019/12/26 10:25:52
     * @param order
     * @return java.lang.String
     **/
    @PostMapping("/add")
    public String add(Order order){
        orderService.add(order);
        return "";
    }

    @PostMapping("/pay")
    @ResponseBody
    public String doPay(String orderId){
        Order order = orderService.getById(orderId);
        PayBean payBean = new PayBean();
        payBean.setTradeNo(orderId);
        payBean.setTotalAmount(order.getTotalMoney().toString());
        payBean.setSubject(order.getAccept()+"--"+order.getAcceptTelephone());
        payBean.setBody("支付订单。");

        return payService.pay(payBean);
    }

    @RequestMapping("/pay/sucpage")
    public String getPaySucpage(){
        return "pay_suc";
    }

    @RequestMapping("/pay/suc")
    public void paySuc(HttpServletRequest request){
        Map<String, String[]> parameterMap = request.getParameterMap();
        parameterMap.forEach((s, strings) -> System.out.println(s+"="+strings));

        String orderId = parameterMap.get("tradeNo")[0];
        //取消等待用户支付的订单任务
        scanOrderStatusService.cancleTask(orderId);


        //给商户发消息
        Order order = orderService.getById(orderId);
        Message message = new Message();
        message.setTitle("你有新的订单啦。");
        message.setContent("订单地址为："+order.getAddress());
        message.setOrderId(orderId);
        message.setBusinessId(order.getBusinessId());
        message.setStatus("1");
        messageService.add(message);


        TextMessage textMessage = new TextMessage(JSON.toJSONString(message));
        zdsWebsocketHandler.sendMessageToBusiness(order.getBusinessId(),textMessage);

        //添加等待商户确认的订单任务
        scanOrderStatusService.addOrder(System.currentTimeMillis()+600000, orderId);
    }


    @PostMapping("/finish/{id}")
    public String finish(@PathVariable String orderId){
        orderService.finish(orderId);
        return "";
    }

    @GetMapping("/orderpage")
    public String getOrder(){
        return "order";
    }
    @GetMapping("/ordersucpage")
    public String getOrderSuc(){
        return "order_success";
    }

    @GetMapping("countdown")
    public String getCountDown(){
        return "countdown";
    }

}
